package com.oocl.servlet.http;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.oocl.memory.SystemMemory;
import com.oocl.pojo.Protocol;
import com.oocl.servlet.action.LoginServletAction;
import com.oocl.util.DESUtil;
import com.oocl.util.JSONUtil;

/**
 * 平台发送拉黑信息
 */
@WebServlet("/sendBanState")
public class GetBanStateServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private SystemMemory systemMemory;
	private Logger logger = Logger.getLogger(GetBanStateServlet.class);
    public GetBanStateServlet() {
        super();
        systemMemory.getInstance();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		response.setCharacterEncoding("UTF-8");
		PrintWriter writer = response.getWriter();
		String mIdSource = request.getParameter("mId").replaceAll(" ", "+");
		DESUtil desUtil = new DESUtil();
		String mId = desUtil.decrypt(mIdSource,"UTF-8");
		String isBanString = request.getParameter("isBan");
		logger.info("mId:"+mId+",isBan:"+isBanString);
		boolean isBan = Boolean.valueOf(isBanString);
		if (mId!=null&&!mId.equals("")) {
			String ban = (String) session.getAttribute("ban");
			if (isBan) {
				systemMemory.addBanList(mId);
				if (ban==null) {
					session.setAttribute("ban", "black");
				}
			}else {
				if (ban!=null) {
					session.removeAttribute("ban");
				}
				systemMemory.removeBanList(mId);
			}
		}
		Protocol protocol = new Protocol();
		String json = JSONUtil.toJSON(protocol);
		writer.write(json);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
